package com.example.cici.cclight;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import com.example.cici.cclight.adapter.FragmentAdapter;
import com.example.cici.cclight.fragment.*;

import java.util.ArrayList;
import java.util.List;

public class thrid_activity extends FragmentActivity implements ViewPager.OnPageChangeListener,TabHost.OnTabChangeListener,OnColorChangeListener {

    private FragmentTabHost mTabHost;
    private LayoutInflater layoutInflater;
    private Class fragmentArray[] = {BlockChangeFragment.class,
    CircleFragment.class,BreathFragment.class,ShutdownFragment.class};//MeumFragment.class,
    private int imageViewArray[] = {R.drawable.select_blockchange,
            R.drawable.select_circle,R.drawable.select_breath,R.drawable.select_shutdown};//R.drawable.select_meum,
    private String textViewArray[] = {"选色","炫彩","呼吸","关灯"};//"菜单",
    private List<Fragment> list = new ArrayList<Fragment>();
    private ViewPager vp;
    private ImageView imageView;
    private GradientDrawable gradientDrawable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);
        imageView = (ImageView) findViewById(R.id.iv_change_color);
        initView();
        intiPage();

    }


    private void intiPage() {
//        MeumFragment fragment1 = new MeumFragment();
        BlockChangeFragment fragment2 = new BlockChangeFragment();
        CircleFragment fragment3 = new CircleFragment();
        BreathFragment fragment4 = new BreathFragment();
        ShutdownFragment fragment5 = new ShutdownFragment();
        fragment2.setOnColorChangeListener(new OnColorChangeListener() {
            @Override
            public void onChange(int r, int g, int b) {
                gradientDrawable = (GradientDrawable) imageView.getBackground();
                gradientDrawable.setColor(Color.rgb(r,g,b));
                Log.e("onChange",r+g+b+"");
            }
        });
//        list.add(fragment1);
        list.add(fragment2);
        list.add(fragment3);
        list.add(fragment4);
        list.add(fragment5);
        //绑定fragment适配器
        vp.setAdapter(new FragmentAdapter(getSupportFragmentManager(),list));
        mTabHost.getTabWidget().setDividerDrawable(null);
    }

    private void initView() {
        vp = (ViewPager) findViewById(R.id.pager);
        /*实现OnPageChangeListener接口,目的是监听Tab选项卡的变化，然后通知ViewPager适配器切换界面*/
        /*简单来说,是为了让ViewPager滑动的时候能够带着底部菜单联动*/
        vp.addOnPageChangeListener(this);//设置页面切换时的监听器
        layoutInflater = LayoutInflater.from(this);//加载布局管理器
        /*实例化FragmentTabHost对象并进行绑定*/
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);//绑定tahost
        mTabHost.setup(this, getSupportFragmentManager(), R.id.pager);//绑定viewpager
        /*实现setOnTabChangedListener接口,目的是为监听界面切换），然后实现TabHost里面图片文字的选中状态切换*/
        /*简单来说,是为了当点击下面菜单时,上面的ViewPager能滑动到对应的Fragment*/
        mTabHost.setOnTabChangedListener(this);
        int count = textViewArray.length;
        /*新建Tabspec选项卡并设置Tab菜单栏的内容和绑定对应的Fragment*/
        for (int i = 0; i < count; i++) {
            // 给每个Tab按钮设置标签、图标和文字
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(textViewArray[i])
                    .setIndicator(getTabItemView(i));
            // 将Tab按钮添加进Tab选项卡中，并绑定Fragment
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
            mTabHost.setTag(i);
            mTabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.selector_tab_background);//设置Tab被选中的时候颜色改变
        }
    }

    private View getTabItemView(int i) {
        //将xml布局转换为view对象
        View view = layoutInflater.inflate(R.layout.tab_content, null);
        //利用view对象，找到布局中的组件,并设置内容，然后返回视图
        ImageView mImageView = (ImageView) view
                .findViewById(R.id.tab_imageview);
        TextView mTextView = (TextView) view.findViewById(R.id.tab_textview);
        mImageView.setBackgroundResource(imageViewArray[i]);
        mTextView.setText(textViewArray[i]);
        return view;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        TabWidget widget = mTabHost.getTabWidget();
        int oldFocusability = widget.getDescendantFocusability();
        widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);//设置View覆盖子类控件而直接获得焦点
        mTabHost.setCurrentTab(position);//根据位置Postion设置当前的Tab
        widget.setDescendantFocusability(oldFocusability);//设置取消分割线
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onTabChanged(String tabId) {
        //Tab改变的时候调用
        int position = mTabHost.getCurrentTab();
        vp.setCurrentItem(position);//把选中的Tab的位置赋给适配器，让它控制页面切换
    }

    @Override
    public void onChange(int r, int g, int b) {
    }
}
